package com.lhy.controller;

import com.lhy.domain.Member;
import com.lhy.domain.User;
import com.lhy.service.MemberService;
import com.lhy.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class MemberController {
    @Autowired
    MemberService memberService;

    //    @RequestMapping("/showAllMember")
//    public String showAllMember(HttpServletRequest req, HttpServletResponse resp){
//        List<Member> members=memberService.showAllMember();
//        HttpSession session = req.getSession();
//        session.setAttribute("members",members);
//        return "memberlist";
//    }
    @RequestMapping("/addMember")
    public String addMember(HttpServletRequest req, HttpServletResponse resp) {
        Member member = new Member();
        User u = new User();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(member, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        try {
            BeanUtils.populate(u, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        memberService.addMember(member, u);
        return "main";
    }

    @RequestMapping("/getMemberId")
    public String getMemberId(HttpServletRequest req, HttpServletResponse resp) {
        int memberId = Integer.parseInt(req.getParameter("memberId"));
        req.setAttribute("id", memberId);
        return "membermodify";
    }

    @RequestMapping("/getMemberId1")
    public String getMemberId1(HttpServletRequest req, HttpServletResponse resp) {
        int memberId = Integer.parseInt(req.getParameter("memberId"));
        req.setAttribute("id", memberId);
        return "membercz";
    }

    @RequestMapping("/deleteMember")
    public String deleteMember(HttpServletRequest req, HttpServletResponse resp) {
        int memberId = Integer.parseInt(req.getParameter("memberId"));
        memberService.deleteMember(memberId);
        return "main";
    }

    @RequestMapping("/modifyMember")
    public String modifyMember(HttpServletRequest req, HttpServletResponse resp) {
        Member member = new Member();
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(member, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        memberService.modifyMember(member);
        return "main";
    }

    @RequestMapping("/czMember")
    public String czMember(HttpServletRequest req, HttpServletResponse resp) {
        Integer memberId = Integer.parseInt(req.getParameter("id"));
        Double rechargeMount = Double.parseDouble(req.getParameter("rechargeMount"));
        memberService.modifyMount(rechargeMount, memberId);
        return "main";
    }

    @RequestMapping("/selectMember")
    public String selectMember(Member member, Pagination<Member> page, HttpServletRequest req, HttpServletResponse resp) {
        page.setQueryObject(member);
        List<Member> members = memberService.showSomeMember(page);
        HttpSession session = req.getSession();
        session.setAttribute("members", page);
        return "memberlist";
    }
}
